package com.my.learn.nio.netty.longConnection;

import io.netty.channel.*;
import io.netty.handler.timeout.IdleStateEvent;

import java.util.HashMap;
import java.util.Map;

/**
 * @author fu
 * @version 1.0
 * @description TODO
 * @date 2021-11-05 13:56
 */
public class KeepLiveHanlder extends ChannelInboundHandlerAdapter {

    private static Map<Integer, Channel> channelCacheMap = new HashMap<>();

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        Channel channel = ctx.channel();
        final int hashCode = channel.hashCode();
        System.out.println("channel hashCode:" + hashCode + " msg:" + msg + " cache:" + channelCacheMap.size());
        if (!channelCacheMap.containsKey(hashCode)) {
            System.out.println("channelCacheMap.containsKey(hashCode), put key:" + hashCode);
            channelCacheMap.put(hashCode, channel);
        }
        if (msg instanceof SendModel) {
            SendModel msg1 = (SendModel) msg;
            switch (msg1.getType()) {
                case 0:
                    SendModel sendModel = new SendModel();
                    sendModel.setType(0);
                    sendModel.setSize(1);
                    sendModel.setContent("收到，你好呀");
                    System.out.println("发送信息:" + sendModel.toString());
                    ctx.channel().writeAndFlush(sendModel);
                    break;
                case 1:
                    sendModel = new SendModel();
                    sendModel.setType(0);
                    sendModel.setSize(1);
                    sendModel.setContent("你不好呀");
                    System.out.println("发送信息:" + sendModel.toString());
                    ctx.channel().writeAndFlush(sendModel);
                    break;
                default:
            }
        }
    }

    @Override
    public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
        if (evt instanceof IdleStateEvent) {
            IdleStateEvent event = (IdleStateEvent) evt;
            System.out.println("evt:" + event.toString());
            switch (event.state()) {
                case READER_IDLE:
                    System.out.println("读空闲");
                    ctx.close();
                    break;
                case WRITER_IDLE:
                    System.out.println("写空闲");
//                    SendModel sendModel = new SendModel();
//                    sendModel.setType(0);
//                    sendModel.setSize(1);
//                    sendModel.setContent("心跳包");
//                    ctx.channel().writeAndFlush(sendModel);
                    break;
                default:
                    break;
            }
            super.userEventTriggered(ctx, evt);
        }
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        if (null != cause) {
            cause.printStackTrace();
        }
        if (null != ctx) {
            ctx.close();
        }
    }
}
